package com.bins.es.controller;

import com.bins.es.common.ResultBody;
import com.bins.es.eo.UserModel;
import com.bins.es.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/user/{id}")
    @ResponseBody
    public UserModel getUserById(@PathVariable Long id) {
        Optional<UserModel> opt = userService.findById(id);
        return opt.get();
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultBody<UserModel> Save(@RequestBody UserModel user) {
        if (user == null) {
            user = new UserModel(1L, "Andybin", "男", 21);
        }
        return userService.save(user);
    }

}
